package cc.angis.xiugallery.util;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import cc.angis.xiugallery.R;
import cc.angis.xiugallery.common.GlobalConst;
import cc.angis.xiugallery.data.CommentData;
import cc.angis.xiugallery.db.CommentDBHelper;
import cc.angis.xiugallery.db.TableConstants;
import cc.angis.xiugallery.image.provider.Images;
import cc.angis.xiugallery.image.util.ImageCache;
import cc.angis.xiugallery.image.util.ImageFetcher;
import cc.angis.xiugallery.image.util.ImageResizer;
import cc.angis.xiugallery.image.util.ImageWorker;
import cc.angis.xiugallery.image.util.ImageCache.ImageCacheParams;

public class CommentLoader {
	
	private Context context;
	
	private ImageResizer mImageWorker;
	
	private static final String IMAGE_CACHE_DIR = "thumbs";
	
	public CommentLoader(Context context){
		this.context = context;
		mImageWorker = new ImageFetcher(context, context.getResources().getDimensionPixelSize(R.dimen.postlist_avatarwidth));
//        mImageWorker.setAdapter(Images.imageThumbWorkerUrlsAdapter);
        mImageWorker.setLoadingImage(R.drawable.empty_photo);
        ImageCacheParams cacheParams = new ImageCacheParams(IMAGE_CACHE_DIR);
        mImageWorker.setImageCache(ImageCache.findOrCreateCache((FragmentActivity)context, cacheParams));
	}
	
	public void loadLastCommentByPostId(String postId, View commentView){
		new LoadLastCommentTask(postId, commentView).execute();
	}
	
	private class LoadLastCommentTask extends AsyncTask<String, Void, CommentData>{
		private String postId;
		
		private View commentView;
		
		
		public LoadLastCommentTask(String postId, View commentView){
			this.postId = postId;
			this.commentView = commentView;
		}

		@Override
		protected CommentData doInBackground(String... params) {
			CommentDBHelper dbHelper = new CommentDBHelper(context, TableConstants.TABLE_COMMENT);
			try{
				return dbHelper.getLastCommentByPostId(postId);
			} catch(Exception e){
				e.printStackTrace();
			} finally{
				dbHelper.closeDB();
			}
			return null;
		}

		@Override
		protected void onPostExecute(CommentData result) {
			super.onPostExecute(result);
			LayoutInflater inflater = (LayoutInflater)((Activity)context).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			if (result == null){
				((ViewGroup)commentView).addView(inflater.inflate(R.layout.nocomment, null));
				return;
			}
			View v = inflater.inflate(R.layout.commentitem, null);
			ImageView avatarView = (ImageView) v.findViewById(R.id.avatarView);
			TextView usernameText = (TextView) v.findViewById(R.id.usernametext);
			TextView timeText = (TextView) v.findViewById(R.id.timetext);
			TextView commentText = (TextView) v.findViewById(R.id.commenttext);
			ImageView platformIcon = (ImageView) v.findViewById(R.id.platformicon);
			mImageWorker.loadImage(result.getAvatar(), avatarView);
			usernameText.setText(result.getAuthor());
			timeText.setText(result.getShowTime());
			commentText.setText(result.getContent());
			switch (result.getType()){
				case GlobalConst.AccountType.ACCOUNT_TYPE_WEIBO:
					platformIcon.setImageResource(R.drawable.weiboicon);
					break;
				case GlobalConst.AccountType.ACCOUNT_TYPE_TWITTER:
					platformIcon.setImageResource(R.drawable.twittericon);
					break;
				case GlobalConst.AccountType.ACCOUNT_TYPE_QQ:
					platformIcon.setImageResource(R.drawable.qqicon);
					break;
				default:
					break;
			}
			((ViewGroup)commentView).addView(v);
			
		}
		
	}

}
